package com.example.hms.service.serviceImpl;
import com.example.hms.Dao.LiveRecordsDao;
import com.example.hms.entity.LiveRecords;
import com.example.hms.service.LiveRecordsService;
import com.example.hms.ulit.Result;
import com.example.hms.ulit.ResultSubmit;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED, rollbackFor = Exception.class)
public class LiveRecordsServiceImpl implements LiveRecordsService{
    @Resource
    LiveRecordsDao liveRecordsDao;
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @Override
    public Result selectAllLiveRecords(String customerName, String roomNumber, String identificationCard) {
        List<LiveRecords> liveRecords = liveRecordsDao.selectAllLiveRecords(customerName, roomNumber, identificationCard);
        return ResultSubmit.success(liveRecords);
    }
    @Override
    public int insert(LiveRecords liveRecords){
        liveRecords.setDataUpdateTime(format.format(new Date()));
        liveRecords.setLiveId(Math.abs((int)System.currentTimeMillis()));
        liveRecords.setDeleteIdentity("未删除");
        int insert=liveRecordsDao.insert(liveRecords);
        return insert;
    }
    @Override
    public Result update(LiveRecords liveRecords) {
        liveRecords.setDataUpdateTime(format.format(new Date()));
        int update=liveRecordsDao.update(liveRecords);
        if (update!=0){
            return ResultSubmit.success("修改成功");
        }else {
            // 事务回滚(手动回滚)
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResultSubmit.error("修改失败");
        }
    }
    @Override
    public Result delete(LiveRecords liveRecords) {
        int delete=liveRecordsDao.update(liveRecords);
        if (delete==1){
            return ResultSubmit.success("删除成功");
        }else {
            // 事务回滚(手动回滚)
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResultSubmit.error("删除失败");
        }
    }
}
